home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / util / arc / AmySTP16c_src.lha / AmyStpDecoder.c < prev    next >
C/C++ Source or Header  |  1997-06-05  |  4KB  |  137 lines

  1. /* **************************************************************
  2.    *                                *
  3.    *                    STP Decoder Program                     *
  4.    *                                *
  5.    *  Original version by: Mnp (unknown)                        *
  6.    *  Amiga (optimized) version by: Manolis S Pappas            *
  7.    *                                Thermopilon 24              *
  8.    *                    14231 Nea Ionia        *
  9.    *                    Athens GREECE        *
  10.    *                                *
  11.    *  E-mail:                            *
  12.    *        mpappas@posidon.servienet.ariadne-t.gr        *
  13.    *            mpappas@acropolis.net                           *
  14.    *        2:410/128.19                    *
  15.    *        39:250/3.19                    *
  16.    **************************************************************
  17. */
  18.  
  19. /* Version 1.6b */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25.  
  26. #define false(a) (((a)<32)||(((a)>123) && ((a)<179))||((a)>216))
  27. #define R_MARGIN 65
  28.  
  29. #define VERSION "1.6c"
  30. static const char CLI_VERS[]="$VER: AmySTP Decoder v1.6c "__AMIGADATE__;
  31.  
  32.  int err(int e)
  33.  {
  34.     char *errlist[12];
  35.  
  36.     errlist[0]="Cannot open input file";
  37.     errlist[1]="Cannot create output file";
  38.  
  39.     errlist[3]="Cannot open input file";
  40.     errlist[4]="Cannot locate STP header";
  41.     errlist[5]="Cannot locate original Filename or Size";
  42.     errlist[6]="Cannot locate original num. of chars and checksum";
  43.     errlist[7]="Cannot create output file";
  44.     errlist[8]="Input file terminated too soon";
  45.     errlist[9]="Internal error";
  46.     errlist[10]="Checksums do not match";
  47.  
  48.    fprintf(stderr,"Error(%d): %s\n",e,errlist[e-2]);
  49.    return e;
  50.  }
  51.  
  52.  
  53.  main(int argc,char *argv[])
  54.  {
  55.      FILE *fin, *fout;
  56.      unsigned char z=R_MARGIN,x,n=7,i,j,a,b[7],c[8],terminate=0;
  57.      unsigned int part=1,sum=1,l;
  58.      unsigned long checksum=0,fsize=0,chars=0,newsum=0;
  59.      char s[80]="",name[80]="";
  60.      char *string[5];
  61.  
  62.  string[0]="START STP SCRIPT %u OF %u\n";
  63.  string[1]="FILENAME : %s - FILESIZE : %lu\n";
  64.  string[2]="CHARACTERS : %lu - CHECKSUM : %lX\n";
  65.  string[3]="\nEND STP SCRIPT\n";
  66.  string[4]="Successful. Checksum :";
  67.  
  68.    if (argc<2)
  69.     { 
  70.       fprintf(stderr,"\nAmySTP Binary Mail Decoder "__AMIGADATE__
  71.               ".\nCopyright © 1994-97, Infinity Labs Development. All Rights Reserved.\n"
  72.               "Author: Manolis S. Pappas. Version: %s\n",VERSION);
  73.       fprintf(stderr,"\nUsage: %s [input text file] \n",argv[0]);
  74.       exit(4);
  75.     }
  76.     if ((fin=fopen(argv[1],"r"))==NULL) exit(err(5));
  77.  
  78.     while (! fscanf(fin,string[0],&part,&sum))
  79.     {
  80.         fgets(s,80,fin);
  81.         if (feof(fin)) exit(err(6));
  82.     }
  83.  
  84.  
  85.     if (! fscanf(fin,string[1],name,&fsize))
  86.        exit(err(7));
  87.     if (! fscanf(fin,string[2],&chars,&checksum))
  88.        exit(err(8));
  89.  
  90.     fprintf(stderr,"Now decoding %u of %u part(s)\n",part,sum);
  91.     fprintf(stderr,"Filename : %s - Original file size : %lu\n",name,fsize);
  92.     fprintf(stderr,"Characters : %lu - Checksum : %lX\n", chars,checksum);
  93.  
  94.     if ((fout=fopen(name,"wb"))==NULL) exit(err(9));
  95.  
  96.     do { c[0]=getc(fin); } while false(c[0]);
  97.     if (!chars--) terminate=1;
  98.     (c[0]>178)?(c[0]-=89):(c[0]-=33);
  99.     while (!terminate)
  100.     {
  101.         for (i=1;i<8;i++)
  102.         {
  103.             do
  104.             {
  105.                 c[i]=getc(fin);
  106.                 if (feof(fin))
  107.                 {   remove(name);
  108.                     exit(err(10));
  109.                 }
  110.             } while false(c[i]);
  111.             if (!chars--) terminate=1;
  112.             if (terminate) n--;
  113.             else (c[i]>178)?(c[i]-=89):(c[i]-=33);
  114.         }
  115.                  b[0]=(c[0]<<1)|(c[1]>>6);
  116.                  b[1]=((c[1]&63)<<2)|(c[2]>>5);
  117.                  b[2]=((c[2]&31)<<3)|(c[3]>>4);
  118.                  b[3]=((c[3]&15)<<4)|(c[4]>>3);
  119.                  b[4]=((c[4]& 7)<<5)|(c[5]>>2);
  120.                  b[5]=((c[5]& 3)<<6)|(c[6]>>1);
  121.                  b[6]=((c[6]& 1)<<7)|c[7];
  122.         for (i=0;i<n;i++)
  123.         { putc(b[i],fout); newsum+=b[i]; }
  124.         do { c[0]=getc(fin); } while false(c[0]);
  125.         if (!chars--) terminate=1;
  126.         (c[0]>178)?(c[0]-=89):(c[0]-=33);
  127.     }
  128.     fclose(fout);
  129.     if (checksum!=newsum)
  130.     {
  131.         remove(name);
  132.         exit(err(12));
  133.     }
  134.     fprintf(stderr,"File Decoding %s %lX\n",string[4],newsum);
  135.     return 0;
  136.  }
  137.